package com.project.api;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@EnableAsync
//@EnableRedisHttpSession
@ComponentScan({"com.project.api.common","com.project.api.ym"})
//@ComponentScan(basePackages = {"com.project.api.common","com.project.api.ym"})
//@EnableAspectJAutoProxy
//@EnableConfigurationProperties(K3CloudAutoConfig.class)
public class BaseApplication {
    @Bean(name="mainDataSource")
    @Primary//将当前连接池作为默认的
    @ConfigurationProperties(prefix="spring.datasource.main")
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }
//    @Bean(name="saleDataSource")
//    @ConfigurationProperties(prefix="spring.datasource.sale")
//    public DataSource testDataSource(){
//        return DataSourceBuilder.create().build();
//    }
//    @Bean(name="yunshangDataSource")
//    @ConfigurationProperties(prefix="spring.datasource.yunshang")
//    public DataSource twoDataSource(){
//        return DataSourceBuilder.create().build();
//    }
    @Bean(name = "mainJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("mainDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
//    @Bean(name = "saleJdbcTemplate")
//    public JdbcTemplate testJdbcTemplate(
//            @Qualifier("saleDataSource") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }
//
//    @Bean(name = "yunshangJdbcTemplate")
//    public JdbcTemplate twoJdbcTemplate(
//            @Qualifier("yunshangDataSource") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }
    //配置事务管理器
//    @Bean(name="mainTransactionManager")
//    public PlatformTransactionManager mainTransactionManager(@Qualifier("mainDataSource") DataSource prodDataSource) {
//        return new DataSourceTransactionManager(prodDataSource);
//    }
//    @Bean(name="yunshangTransactionManager")
//    public PlatformTransactionManager yunshangTransactionManager(@Qualifier("yunshangDataSource") DataSource prodDataSource) {
//        return new DataSourceTransactionManager(prodDataSource);
//    }
    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(BaseApplication.class, args);
//        try {
//            context.getBean(Task.class).getYunShangOrder();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            context.close();
//        }
    }

}
